package com.ivy.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ivy.dao.TContentDao;
import com.ivy.entity.TContent;

import com.ivy.utils.JdbcUtils;

public class TContentDaoImpl implements TContentDao {

	@Override
	public void insert(List<TContent> tcontents) {
		try {
			for(TContent tcontent:tcontents){
				if(tcontent.getInfo().length()<5){
					continue;
				} else{
					
					String sql = "insert into tcontent(info,weekday,time,tid) values(?,?,?,?);";
					QueryRunner qr = JdbcUtils.getQueryRunner();
					qr.update(sql,tcontent.getInfo(),tcontent.getWeekday(),tcontent.getTime(),tcontent.getTid());
					System.out.println(sql);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	@Override
	public List<TContent> findAllTContentByTid(String tid) {
		String sql = "select * from tcontent where tid = '" + tid + "';";
		List<TContent> tcontents = null;
		try {
			QueryRunner qr = JdbcUtils.getQueryRunner();
			tcontents = qr.query(sql, new BeanListHandler<TContent>(TContent.class));
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return tcontents;
		
	}

}
